$(function() {
	$( "#accordion" ).accordion({
		collapsible: true
	});
	
 
	 $('.product-box-get-info').click(function () {
		 $("#loading-message").text("Loading Product Information. Please wait.");
		 $("#loading-box").dialog("open");
		 var $elem = $(this);
		 var $product_id = $($elem).children().filter("#product_id").val();;
		 
		 var request = {
				 'product_id' : $product_id 
		 };
		 
		 $.post('index.php?c=product&m=getProductInformation', request, function (response) {
			 var obj = jQuery.parseJSON(response);
			 
			 $('#text_product_id').text(obj.product_id);
			 $('#product_image').attr('src', obj.product_image);
			 $('#file_product_image').text(obj.product_image);
			 
			 $('#text_product_name').val(obj.product_name);
			 $('#text_product_price').val(obj.product_price);
			 $('#text_product_description').val(obj.product_description);
					 
			 //var category = '#select_product_category option[value="' + obj.product_category_id + '"]';
			 //var archive = '#select_product_archive option[value="' + obj.product_archive + '"]';
			 //var image = '#availImg option[value="' + obj.product_image + '"]';
			 			 
			 if(obj.product_archive == 'y'){
				 $('#unarchive_btn').css('visibility','visible');
			 } else {
				 $('#unarchive_btn').css('visibility','hidden');
			 }
			 
			 $('#product_btn_save').css('visibility','visible');
			 $('#availImg').css('visibility','visible');
			 
			 //$(category).attr('selected','selected');
			 //$(archive).attr('selected','selected');
			 //$(image).attr('selected','selected');
			 
			 $("#select_product_category").val(obj.product_category_id);
			 $("#select_product_archive").val(obj.product_archive);
			 $("#availImg").val(obj.product_image);
			 
			 $("#loading-box").dialog("close");
		 });
	 });
	 
	 $('.form_product_info').submit(function () {
		 var product_id = $('#text_product_id').text();
		 var product_name = $('#text_product_name').val();
		 var product_category = $('#select_product_category').val();
		 var product_price = $('#text_product_price').val();
		 var product_description = $('#text_product_description').val();
		 var product_archive = $('#select_product_archive').val();
		 var result = true;
		 
		 if (product_id != '') {
			 result = false;
		 }
		 
		 return result;
	 });
	 
	 $('#product_box_dialog').dialog({
		 modal: true
	 });
	 
	 $('#product_box_dialog').dialog("close");
	 
	 $("#product_btn_add").click(function () {
		 //get available images
		 var url = getUrl('product', 'getAvailableImages');
		 var request = {}
		 
		 $.post(url, request, function(result) {
				$("#availImg_add").html(result);
				$('#product_box_dialog').dialog("open");
			}).fail(function() {alert("Something went wrong");$('#product_box_dialog').dialog("open");});
	 });
	 
	 $("#availImg_add").change(function () {
		 var imageUrl = $("#availImg_add").val();
		 $("#file_product_image_add").text(imageUrl);
		 $("#product_image_add").attr('src',imageUrl);
	 });
	 
	 $("#availImg").change(function () {
		 var imageUrl = $("#availImg").val();
		 $("#file_product_image").text(imageUrl);
		 $("#product_image").attr('src',imageUrl);
	 });
	 
	 
	 
	 $('#text_product_name').keyup(function() {
         if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
             this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
         }
     });
	 
	 $('#text_product_description').keyup(function() {
         if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
             this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
         }
     });
	 
	 $('#text_product_price').keyup(function() {
         if (this.value.match(/[^0-9]/g)) {
             this.value = this.value.replace(/[^0-9]/g, '');
         }
     });
	 
	 $('#text_product_price_add').keyup(function() {
         if (this.value.match(/[^0-9]/g)) {
             this.value = this.value.replace(/[^0-9]/g, '');
         }
     });
	 
	 $('#text_product_count_add').keyup(function() {
         if (this.value.match(/[^0-9]/g)) {
             this.value = this.value.replace(/[^0-9]/g, '');
         }
     });
	 
	 $('#text_product_name_add').keyup(function() {
         if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
             this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
         }
     });
	 
	 $('#text_product_description_add').keyup(function() {
         if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
             this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
         }
     });
	 
	 getImages();
});

function getImages(){
	//get available images
	 var url = getUrl('product', 'getAvailableImages');
	 var request = {}
	 
	 $.post(url, request, function(result) {
			$("#availImg").html(result);
		}).fail(function() {alert("Something went wrong");});
}

function showName(prodId){
	hideName();
	$("#prodName_"+prodId).slideDown();	
}

function hideName(){
	$(".product-image-name").slideUp();
}

function updateProduct(){
	var productId = $("#text_product_id").text();
	var productName =  $("#text_product_name").val();
	var category = $("#select_product_category").val();
	var price = $("#text_product_price").val();
	var description = $("#text_product_description").val();
	var archive = $("#select_product_archive").val();
	var img = $("#file_product_image").text();
	
	var url = getUrl('product', 'updateProduct');
	
	var request = {
			productId: productId,
			productName: productName,
			productPrice: price,
			description: description,
			archive: archive,
			category: category,
			img: img
	}
	
	if($.trim(productName) != "" && price > 0){
		$("#loading-message").text("Updating Product Information. Please wait.");
		$("#loading-box").dialog("open");
		
		$.post(url, request , function(result,status) {
			
			$("#loading-box").dialog("close");
			
			if(result.rowsAffected > 0){
				$("#alert-box").dialog({
				    buttons: {
				        "OK" : function () {
				        	var url2 = getUrl('product', 'index');
				        	window.location.href = url2;
				        }
				    }
				});
				$("#alert-message").text("Update Successful!");
			} else {
				$("#alert-message").text("Nothing has been changed!");
			}
			$("#alert-box").dialog("open");
			
		},"json").fail(function() { $("#loading-box").dialog("close"); });
	}
	
}

function unarchive(){
	var productId = $("#text_product_id").text();
	
	var url = getUrl('product', 'unarchive');
	
	var request = {
			productId: productId
	}
	

		$("#loading-message").text("Unarchiving Product. Please wait.");
		$("#loading-box").dialog("open");
		
		$.post(url, request , function(result,status) {
			
			$("#loading-box").dialog("close");
			
			if(result.rowsAffected > 0){
				$("#alert-box").dialog({
				    buttons: {
				        "OK" : function () {
				        	var url2 = getUrl('product', 'index');
				        	window.location.href = url2;
				        }
				    }
				});
				$("#alert-message").text("Unarchive Successful!");
			} else {
				$("#alert-message").text("Nothing has been changed!");
			}
			$("#alert-box").dialog("open");
			
		},"json").fail(function() { $("#loading-box").dialog("close"); });
}

function addProduct(){

	var productName =  $("#text_product_name_add").val();
	var price = $("#text_product_price_add").val();
	var description = $("#text_product_description_add").val();
	var count = $("#text_product_count_add").val();
	var category = $("#select_product_category_add").val();
	var img = $("#file_product_image_add").text();
		
	var url = getUrl('product', 'addProduct');
	
	var request = {
			productName: productName,
			productPrice: price,
			description: description,
			count: count,
			category: category,
			img: img
	}
	
	if($.trim(productName) != "" && price > 0){
		$("#loading-message").text("Adding Product Information. Please wait.");
		$("#loading-box").dialog("open");
		
		$.post(url, request , function(result,status) {
			
			$("#loading-box").dialog("close");
			
			if(result.rowsAffected > 0){
				$("#alert-box").dialog({
				    buttons: {
				        "OK" : function () {
				        	var url2 = getUrl('product', 'index');
				        	window.location.href = url2;
				        }
				    }
				});
				$("#alert-message").text("Add Product Successful!");
			} else {
				$("#alert-message").text("Nothing has been changed!");
			}
			$("#alert-box").dialog("open");
			
		},"json").fail(function() { $("#loading-box").dialog("close"); });
	}
}
